package com.dehui.traders.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.dehui.traders.bean.SearchCodeResult;
import com.google.gson.Gson;

public class Preferences{

    private static final String PREFERENCE_NAME = "TJ_STCOK";
    private static final String KEY_USER_TOKEN = "user_token";
    private static final String KEY_USER_PHONE = "user_phone";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USER_NICKNAME = "user_nickname";
    private static final String KEY_HEADIMAGE_URL = "headimage_url";
    private static final String KEY_SEARCH_CODE_LIST = "KEY_SEARCH_CODE_LIST";
    private static final String KEY_OPTION_CODE = "KEY_OPTION_CODE";
    private static Context context;

    public static void setContext(Context context) {
        Preferences.context = context.getApplicationContext();
    }

    public static void savePhone(String account){
        saveString(KEY_USER_PHONE,account);
    }

    public static String getPhone(){
        return getString(KEY_USER_PHONE);
    }

    public static void saveUserId(String userId){
        saveString(KEY_USER_ID,userId);
    }

    public static String getUserId(){
        return getString(KEY_USER_ID);
    }

    public static void saveUserNickName(String nickName){
        saveString(KEY_USER_NICKNAME,nickName);
    }

    public static String getUserNickName(){
        return getString(KEY_USER_NICKNAME);
    }

    public static void saveImageUrl(String iamgeUrl){
        saveString(KEY_HEADIMAGE_URL,iamgeUrl);
    }

    public static String getHeadImageUrl(){
        return getString(KEY_HEADIMAGE_URL);
    }

    public static void saveToken(String token) {
        saveString(KEY_USER_TOKEN, token);
    }

    public static String getToken() {
        return getString(KEY_USER_TOKEN);
    }

    private static void saveString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }
    private static void saveBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private static boolean getBoolen(String key) {
        return getSharedPreferences().getBoolean(key, false);
    }

    private static String getString(String key) {
        return getSharedPreferences().getString(key, null);
    }

    static SharedPreferences getSharedPreferences() {
        return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public static void saveSeachCodeList(String msg) {
        saveString(KEY_SEARCH_CODE_LIST,msg);
    }

    public static String getSeachCodeList(){
        return getSharedPreferences().getString(KEY_SEARCH_CODE_LIST,"");
    }

    public static void saveMyOpitonCode(String codestr) {
        String userAccount = getUserId();
        saveString(KEY_OPTION_CODE + "_" +userAccount,codestr);
    }

    public static String getMyOpitonCode(){
        String userAccount = getUserId();
        return getString(KEY_OPTION_CODE + "_" + userAccount);
    }

}
